package com.meizi.user.service.feign;

import com.meizi.core.tool.api.R;
import com.meizi.core.tool.exception.ValidException;
import com.meizi.core.tool.status.ValidStatus;
import com.meizi.core.tool.utils.JwtGeneratorUtil;
import com.meizi.platformuserapi.clients.IUserAuthFeignClient;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.SignatureException;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author meizi
 * @email enjoymeizi@gmail.com
 * @date 2020/11/26 13:11
 */
@RestController
public class UserAuthFeignClient implements IUserAuthFeignClient {
    @Override
    public R validToken(String token) {
        if(StringUtils.isBlank(token)){
            throw new ValidException(ValidStatus.TOKEN_IS_EMPTY);
        }
        try {
            Claims claims = JwtGeneratorUtil.parseToken(token);
            return new R().ofSuccess(claims.get("uid").toString());
        }catch (ExpiredJwtException e){
            return new R().ofStatus(ValidStatus.TOKEN_IS_EXPIRE);
        }catch (SignatureException e){
            return new R().ofStatus(ValidStatus.TOKEN_SIGNATURE_FAIL);
        }
    }
}
